#ifndef AES_TCP_LIB_H
#define AES_TCP_LIB_H

#include <QtNetwork>
#include <QDebug>
#include <QObject>
#include <QtXml>
#include <QtXml/QDomDocument>
#include <QString>
#include "qt_aes.h"
#include "lus_prase.h"
#include <openssl/rsa.h>
#include "aes_tcp_lib_global.h"


#define ENCRYPT_YES 0x12
#define ENCRYPT_NO	0x7

#define BIG		1
#define LITTLE	2

#define LUS_ENABLE 1
#define LUS_DISABLE 0


enum DOWN_STATUS{
    DOWN_ING= 0,
    DOWN_OK
};

struct lus_msg{

    QByteArray msg;
    QByteArray to_addr;
    quint8 encrypt_c;
};


class AES_TCP_LIB_EXPORT aes_tcp_lib:public QObject
{
   Q_OBJECT
public:
    static aes_tcp_lib * create_aes_tcp();
    void aes_tcp_init(void);
    void aes_tcp_close(void);
    void lus_set_order(qint8 order);
    void update_fix_id();
    void sendMessage(QByteArray b_data, quint8 encrypt_c);
    void prase_terminal_cmd(QDomElement rootnode);
    void lus_send_user_msg(QString type, QString to_addr, QString body);
    void lus_send_raw_msg(QByteArray b_data, quint8 encrypt_c);
    void lus_down_pool_file(QString index, QString label, QString file_name, QString save_path="./");
    void lus_set_debug(bool sw);
    void lus_set_server(QString ip, quint16 port);
    void lus_set_prase(bool sw);
    void lus_set_dst(QString addr);
	void lus_feed_dog(uint32_t feed);
	void lus_config_dog(uint32_t freq, QString path);

    QString lus_get_fix_id(void);
    QString lus_get_var_id(void);
    uint8_t lus_get_down_status(void);
private:
    aes_tcp_lib();
    ~aes_tcp_lib();

    int wd_fd;
    int wd_timeout=10;

    QString server_ip;
    quint16 msg_port=0;
    bool parse_enable = true;
    bool debug_enable = false;
    QString to_addr;
    /****************************/

    RSA *server_pub_rsa;
    QByteArray local_pub_rsa;
    RSA *lus_rsa = nullptr;

    QString fix_id;
    QString from_server_id;
    QString lus_group_id;
    QString lus_version;

    uint8_t down_status = DOWN_OK;

    char *aes_cache;
    UCHAR aes_key[16]={0};
    void newConnect(); //连接服务器
    bool default_do_cmd(QByteArray cmd);
    void default_do_big_cmd(QString cmd);

    int rsa_encrypt(char *str_in, char *str_out);
    int rsa_decrypt(char *str_in, char *str_out);
    lus_prase *parse;
    QTcpSocket *msg_tcp_socket;  //命令传输
    QTcpSocket *big_tcp_socket;  //图像等高占用率传输
    QTcpSocket	*f_tcpSocket;	  //传输文件
    QString file_save_path;
    TAesClass *aes ;

    qint8 byte_order = LITTLE;
    char * tcp_big_data_cache;
    char * tcp_msg_data_cache;

    /*文件传输*/
    void newFileConnect();
    void recive_file_finally(void);
    QString get_file_md5(QString str_path);

    void do_send_msg(QTcpSocket *socket,QByteArray b_data, quint8 encrypt_c);

    quint32 msg_blockSize=0; //存放文件的大小信息
    /*文件传输变量*/
    qint64 file_type;
    qint64 totalBytes;  //存放总大小信息
    qint64 bytesReceived;  //已收到数据的大小
    qint64 fileNameSize;  //文件名的大小信息
    QString *fileName;   //存放文件名
    QFile *localFile;   //本地文件
    QByteArray *inBlock;   //数据缓冲区
    QString *s_file_md5;

    QTimer *timer_connect;
    QTimer *timer_heart;

private slots:

      void displayError(QAbstractSocket::SocketError);  //显示错误
      void readMessage_msg();
     // void readMessage_big();
     // void readMessage(uint8_t select);
      void f_connected(void);
      void enable_reconnect(void);
      void disable_reconnect(void);
      void timer_auto_connect(void);
      void timer_heart_keep(void);
      void do_prase_requst(QByteArray msg);
    void updateServerProgress();

signals:

     void aes_tcp_connected(void);
     void aes_tcp_disconnected(void);
     void read_xml_msg(QDomElement rootnode);
     void read_msg(QByteArray msg);
     void recive_lus_pack(QString file_name);
     void image_ready(QImage *image);
     void down_status_change(uint8_t status);
};


#endif // AES_TCP_LIB_H
